package com.qiyi.player.core.model.remote
{
   import com.qiyi.player.base.rpc.impl.BaseRemoteObject;
   import com.qiyi.player.core.player.coreplayer.ICorePlayer;
   import com.qiyi.player.base.logging.ILogger;
   import com.qiyi.player.base.pub.ProcessesTimeRecord;
   import flash.net.URLRequest;
   import com.qiyi.player.core.model.def.PlayerTypeEnum;
   import com.qiyi.player.base.utils.UGCUtils;
   import com.qiyi.player.core.Config;
   import flash.events.Event;
   import com.adobe.serialization.json.JSON;
   import flash.utils.*;
   import com.qiyi.player.core.model.impls.pub.Settings;
   import com.qiyi.player.base.rpc.RemoteObjectStatusEnum;
   import com.qiyi.player.base.pub.EnumItem;
   import com.qiyi.player.core.model.utils.ErrorCodeUtils;
   import com.qiyi.player.base.logging.Log;
   
   public class VInfoRemote extends BaseRemoteObject
   {
      
      public function VInfoRemote(param1:ICorePlayer) {
         this._log = Log.getLogger("com.qiyi.player.core.model.remote.VInfoRemote");
         super(0,"VInfoRemote");
         this._holder = param1;
         this._holder.runtimeData.authenticationError = false;
         _timeout = 5000;
         _retryMaxCount = 2;
      }
      
      private var _holder:ICorePlayer;
      
      private var _oversea:Boolean = false;
      
      private var _log:ILogger;
      
      override public function initialize() : void {
         ProcessesTimeRecord.STime_VInfo = getTimer();
         super.initialize();
      }
      
      override protected function getRequest() : URLRequest {
         var _loc1_:* = "";
         if(this._holder.runtimeData.playerType == PlayerTypeEnum.SHARE)
         {
            _loc1_ = this._holder.runtimeData.originalVid;
         }
         else if(UGCUtils.isUGC(this._holder.runtimeData.tvid))
         {
            _loc1_ = this._holder.runtimeData.tvid;
         }
         else
         {
            _loc1_ = this._holder.runtimeData.albumId;
         }
         
         return new URLRequest(Config.CHECK_V_INFO_URL + "?id=" + _loc1_ + "&tvid=" + this._holder.runtimeData.tvid + "&tn=" + Math.random());
      }
      
      override protected function onComplete(param1:Event) : void {
         var obj:Object = null;
         var curPlaying_limit:String = null;
         var area:String = null;
         var s:String = null;
         var event:Event = param1;
         clearTimeout(_waitingResponse);
         _waitingResponse = 0;
         ProcessesTimeRecord.usedTime_VInfo = getTimer() - ProcessesTimeRecord.STime_VInfo;
         try
         {
            obj = com.adobe.serialization.json.JSON.decode(_loader.data);
            this._data = obj;
            if(obj.hasOwnProperty("s"))
            {
               this._holder.runtimeData.CDNStatus = int(obj.s);
            }
            else
            {
               this._holder.runtimeData.CDNStatus = 0;
            }
            if(obj.hasOwnProperty("r"))
            {
               this._holder.runtimeData.stratusIP = (obj.r as String).split("//")[1];
            }
            else
            {
               this._holder.runtimeData.openFlashP2P = false;
            }
            if(obj.hasOwnProperty("m"))
            {
               this._holder.runtimeData.smallOperators = obj.m == "1";
            }
            else
            {
               this._holder.runtimeData.smallOperators = false;
            }
            this._holder.runtimeData.dispatchFlashRunTime = int(getTimer() / 1000);
            this._holder.runtimeData.dispatcherServerTime = uint(_data.time);
            this._log.info("VInfoRemote Result: " + _loader.data);
            curPlaying_limit = obj.t;
            area = curPlaying_limit.split("|")[0];
            this._log.info("the user node is in " + area);
            this._oversea = Boolean(area == "OVERSEA");
            if(Settings.instance.boss)
            {
               this._holder.runtimeData.oversea = 0;
            }
            else
            {
               this._holder.runtimeData.oversea = this._oversea?1:0;
            }
            this.setStatus(RemoteObjectStatusEnum.Success);
         }
         catch(e:Error)
         {
            _log.warn("VInfoRemote catched error, when parse json:");
            s = _loader.data;
            if(s)
            {
               _log.info(s.substr(0,100));
            }
            _oversea = false;
            _holder.runtimeData.oversea = 0;
            setStatus(RemoteObjectStatusEnum.DataError);
         }
      }
      
      override protected function setStatus(param1:EnumItem) : void {
         var _loc2_:* = 0;
         if(param1 == RemoteObjectStatusEnum.Timeout || param1 == RemoteObjectStatusEnum.ConnectError || param1 == RemoteObjectStatusEnum.DataError || param1 == RemoteObjectStatusEnum.AuthenticationError || param1 == RemoteObjectStatusEnum.SecurityError || param1 == RemoteObjectStatusEnum.UnknownError)
         {
            _loc2_ = ErrorCodeUtils.getErrorCodeByRemoteObject(this,param1);
            this._holder.pingBack.sendError(_loc2_);
            this._holder.runtimeData.errorCode = _loc2_;
         }
         super.setStatus(param1);
      }
   }
}
